package GUI;


import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Toolkit;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JTextField;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import Codigo.PatronAcceso;
import java.awt.Color;
//import java.awt.Window.Type;

public class FrameControlAcceso extends JFrame {

	private JPanel contentPane;
	private JTextField passwordUsuario;
	private JPasswordField passwordClave;
	private File file = new File("./fondo.jpg");
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					FrameControlAcceso frame = new FrameControlAcceso();
					frame.setVisible(true);
					 frame.setLocationRelativeTo(null);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 * @throws IOException 
	 */
	public FrameControlAcceso() throws IOException {
	 
		setTitle("Control de acceso");
		setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		setBounds(100, 100, 542, 442);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
	   
//		 Toolkit tk = Toolkit.getDefaultToolkit();
//		    Dimension tamano = tk.getScreenSize();
//         setSize(tamano);
 
 	 	    

		
		BufferedImage tmpImagen=ImageIO.read(file); 
		bgBorder fondo = new bgBorder(tmpImagen);
		contentPane.setBorder(fondo);

		
		passwordUsuario = new JTextField();
		passwordUsuario.setBounds(194, 87, 101, 20);
		contentPane.add(passwordUsuario);
		
		JButton boton7 = new JButton("7");
		boton7.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton7.setBounds(51, 162, 82, 51);
		contentPane.add(boton7);
		
		JButton boton8 = new JButton("8");
		boton8.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton8.setBounds(132, 162, 82, 51);
		contentPane.add(boton8);
		
		JButton boton9 = new JButton("9");
		boton9.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton9.setBounds(213, 162, 82, 51);
		contentPane.add(boton9);
		
		JButton boton4 = new JButton("4");
		boton4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton4.setBounds(51, 212, 82, 51);
		contentPane.add(boton4);
		
		JButton boton5 = new JButton("5");
		boton5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton5.setBounds(132, 212, 82, 51);
		contentPane.add(boton5);
		
		JButton boton6 = new JButton("6");
		boton6.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton6.setBounds(213, 212, 82, 51);
		contentPane.add(boton6);
		
		JButton boton1 = new JButton("1");
		boton1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton1.setBounds(51, 262, 82, 51);
		contentPane.add(boton1);
		
		JButton boton2 = new JButton("2");
		boton2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton2.setBounds(132, 262, 82, 51);
		contentPane.add(boton2);
		
		JButton boton3 = new JButton("3");
		boton3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton3.setBounds(213, 262, 82, 51);
		contentPane.add(boton3);
		
		JButton boton0 = new JButton("0");
		boton0.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s + arg0.getActionCommand());
			}
		});
		boton0.setBounds(51, 312, 82, 51);
		contentPane.add(boton0);
		
		JButton btnCe = new JButton("CE");
		btnCe.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				passwordClave.setText("");
			}
		});
		btnCe.setBounds(132, 312, 82, 51);
		contentPane.add(btnCe);
		
		JButton btnBksp = new JButton("Bksp");
		btnBksp.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) 
			{
				char[] pass = passwordClave.getPassword();
				String s = new String(pass);
				passwordClave.setText(s.substring(0,s.length()-1));
			}
		});
		btnBksp.setBounds(213, 312, 82, 51);
		contentPane.add(btnBksp);
		
		JButton botonEntrar = new JButton("Entrar");
		botonEntrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				
				char[] aux = passwordClave.getPassword();
				String password = new String(aux);
				String usuario = passwordUsuario.getText();
				PatronAcceso patron = new PatronAcceso(usuario,password);
				if (patron.iniciaSesion())
				{
					int permisos = patron.establecePermisos();
					InterfazPrimera framePP = null;
					try {
						framePP = new InterfazPrimera(usuario,permisos);
					} catch (IOException e) {
						// TODO Auto-generated catch block5
						e.printStackTrace();
					}
					framePP.setVisible(true);
					framePP.setLocationRelativeTo(null);
				}
			}
		});
		botonEntrar.setBounds(305, 162, 200, 51);
		contentPane.add(botonEntrar);
		//botonEntrar.setBackground(new Color(100,100,200,5));
		
		JButton botonSalir = new JButton("Cierre Sesion");
		botonSalir.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				char[] aux = passwordClave.getPassword();
				String password = new String(aux);
				String usuario = passwordUsuario.getText();
				PatronAcceso patron = new PatronAcceso(usuario,password);
				if (patron.cierraSesion())
				{
					JOptionPane.showMessageDialog(null,"La sesion ha sido cerrada correctamente.");
					System.exit(0);
				}
				else
				{
					JOptionPane.showMessageDialog(null,"La sesion no se ha cerrado correctamente." + "\n" +
							"Vuelva a intentarlo.");
				}
			}
		});
		botonSalir.setBounds(305, 312, 200, 51);
		contentPane.add(botonSalir);
		
		JLabel lblNewLabel = new JLabel("Control de Acceso");
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setFont(new Font("Tahoma", Font.BOLD, 28));
		lblNewLabel.setBounds(142, 21, 275, 34);
		contentPane.add(lblNewLabel);
		
		passwordClave = new JPasswordField();
		passwordClave.setBounds(194, 118, 101, 20);
		contentPane.add(passwordClave);
		
		JLabel lblIntroduzcaUsuario = new JLabel("Introduzca Usuario");
		lblIntroduzcaUsuario.setForeground(Color.WHITE);
		lblIntroduzcaUsuario.setBounds(51, 90, 118, 14);
		contentPane.add(lblIntroduzcaUsuario);
		
		JLabel lblIntroduzcaClave = new JLabel("Introduzca Clave");
		lblIntroduzcaClave.setForeground(Color.WHITE);
		lblIntroduzcaClave.setBounds(51, 115, 118, 14);
		contentPane.add(lblIntroduzcaClave);
	}
}